﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Streambolics.Tablet
{
    public class TouchTrackerEventArgs : EventArgs
    {
        private ITouchTracker _TouchTracker;

        public TouchTrackerEventArgs (ITouchTracker ATracker)
        {
            _TouchTracker = ATracker;
        }

        public ITouchTracker TouchTracker
        {
            get
            {
                return _TouchTracker;
            }
        }
    }

    /// <summary>
    ///     Base class for user controls that track touch events using
    ///     ITouchTracker objects.
    /// </summary>

    public partial class TouchTrackerUserControl : TouchAwareUserControl
    {
        private Dictionary<int, ITouchTracker> _ActiveTrackers = new Dictionary<int, ITouchTracker> ();

        public TouchTrackerUserControl ()
        {
            InitializeComponent ();
        }

        protected void DoTrackerAdded (ITouchTracker t)
        {
            OnTrackerAdded (new TouchTrackerEventArgs (t));
        }

        protected virtual void OnTrackerAdded (TouchTrackerEventArgs e)
        {
        }

        protected void DoTrackerChanged (ITouchTracker t)
        {
            OnTrackerChanged (new TouchTrackerEventArgs (t));
        }

        protected virtual void OnTrackerChanged (TouchTrackerEventArgs e)
        {
        }

        protected void DoTrackerRemoved (ITouchTracker t)
        {
            OnTrackerRemoved (new TouchTrackerEventArgs (t));
        }

        protected virtual void OnTrackerRemoved (TouchTrackerEventArgs e)
        {
        }

        protected virtual ITouchTracker CreateTouchTracker (WMTouchEventArgs e)
        {
            return null;
        }

        protected override void OnTouchDown (WMTouchEventArgs e)
        {
            ITouchTracker t = CreateTouchTracker (e);
            if (t != null)
            {
                t.Down (e);
                _ActiveTrackers.Add (e.Id, t);
                DoTrackerAdded (t);
            }
            base.OnTouchDown (e);
        }

        protected override void OnTouchUp (WMTouchEventArgs e)
        {
            if (_ActiveTrackers.ContainsKey (e.Id))
            {
                _ActiveTrackers[e.Id].Up (e);
                DoTrackerRemoved (_ActiveTrackers[e.Id]);
                _ActiveTrackers.Remove (e.Id);
            }
            base.OnTouchUp (e);
        }

        protected override void OnTouchMove (WMTouchEventArgs e)
        {
            if (_ActiveTrackers.ContainsKey (e.Id))
            {
                _ActiveTrackers[e.Id].Move (e);
                DoTrackerChanged (_ActiveTrackers[e.Id]);
            }
            base.OnTouchMove (e);
        }

        //  The number of active trackers

        public int TrackCount
        {
            get
            {
                return _ActiveTrackers.Count;
            }
        }

        public IEnumerable<ITouchTracker> ActiveTrackers
        {
            get
            {
                return _ActiveTrackers.Values;
            }
        }

    }
}
